package my.suveng.suxinnetty.wsserver;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

/**
 * create at 2018/10/21
 * author: suveng
 * email: suveng@163.com
 **/
@Component
public class Wsserver {

    private  NioEventLoopGroup maingroup;
    private  NioEventLoopGroup sub;
    private  ServerBootstrap serverBootstrap;

    public Wsserver() {
        maingroup = new NioEventLoopGroup();
        sub = new NioEventLoopGroup();
        serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(maingroup, sub)
                .channel(NioServerSocketChannel.class)
                .childHandler(new WsServerInitializer());
    }

    /**
     * netty 启动方法
     */
    public void start(){
        serverBootstrap.bind(8088);
        System.err.println("netty web socket start 启动完毕");

    }

    /**
     * 静态内部类，用于获取单例
     */
    private static class SingleWsserver{
        static final Wsserver ws=new Wsserver();
    }

    /**
     * 获取单例的方法
     * @return 单例
     */
    public static Wsserver getInstance(){
        return SingleWsserver.ws;
    }

}
